package cn.siat.gene.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ZipUtil;
import cn.siat.common.core.exception.ServiceException;
import cn.siat.common.core.utils.StringUtils;
import cn.siat.gene.domain.Feature;
import cn.siat.gene.domain.Programme;
import cn.siat.gene.mapper.ProgrammeMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
@Slf4j
public class BioinformaticsPipelineService {


    private final AsyncBioinformaticsPipelineService asyncBioinformaticsPipelineService;
    private final String separator = File.separator;

    /**
     * 获取基础路径
     *
     * @return
     */
    private String getPrefixPath() {
        String osName = System.getProperty("os.name").toLowerCase();
        String prefixPath = "";
        if (osName.contains("win")) {
            prefixPath = "D:" + separator + "gene" + separator;
        } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) {
            prefixPath = separator + "opt" + separator + "big-folder" + separator + "BioinformaticsPipeline" + separator;
        } else if (osName.contains("mac")) {
            prefixPath = separator + "usr" + separator + "local" + separator;
        } else {
            throw new ServiceException("未知操作系统");
        }
        return prefixPath + "result" + separator;
    }



    /**
     * 基因预测
     * @param params
     * @return
     */
    public ResponseEntity<?> geneExpression(Map<String, String> params) {
        String prefixPath = getPrefixPath();

        String geneid = params.get("geneid");
        String snowId = IdUtil.getSnowflakeNextIdStr();

        String basePath = prefixPath + snowId;
        //生成config.yaml文件
        FileUtil.mkdir(basePath);
        File configYaml = FileUtil.file(basePath + separator + "config.yaml");
        PrintWriter printWriter = null;
        try {
            printWriter = new PrintWriter(configYaml);
            printWriter.println("geneid: \""+geneid+"\"");
        }catch (FileNotFoundException e){
            log.error(e.getMessage());
        } finally {
            if (printWriter != null) {
                printWriter.close();
            }
        }

        asyncBioinformaticsPipelineService.runCommand(basePath,"geneExpression");

        File zip = ZipUtil.zip(basePath+separator+"output",basePath+".zip");
        FileSystemResource fileSystemResource = new FileSystemResource(zip);
        ResponseEntity<InputStreamResource> response = null;
        try {
            response = ResponseEntity
                    .ok()
                    .contentLength(fileSystemResource.contentLength())
                    .contentType(MediaType.parseMediaType("application/octet-stream"))
                    .body(new InputStreamResource(fileSystemResource.getInputStream()));
        } catch (IOException e) {
            throw new ServiceException("下载失败!");
        }finally {
            FileUtil.del(basePath);
            FileUtil.del(zip.getPath());
        }

        return response;
    }


    public void blast2(Map<String, String> params) {
        //分析状态改为运行中

        asyncBioinformaticsPipelineService.asyncBlast(params);
    }






}
